package com.foreverwin.mes.meapi.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.meapi.service.BomComponentService;
import com.foreverwin.mes.meapi.model.BomComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Ervin Chen
 * @since 2020-02-07
 */
@Controller
@RequestMapping("/bomComponents")
public class BomComponentController {
    private final Logger logger = LoggerFactory.getLogger(BomComponentController.class);

    @Autowired
    public BomComponentService bomComponentService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getBomComponentById(@PathVariable String id) {
        BomComponent result;
        try {
            result = bomComponentService.selectById(id);
        } catch (Exception e) {
            logger.error("getBomComponentById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getBomComponentList(BomComponent bomComponent){
        List<BomComponent> result;
        try {
            EntityWrapper<BomComponent> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(bomComponent);
            result = bomComponentService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getBomComponentList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getBomComponentPageList")
    public AjaxResult getBomComponentPageList(FrontPage<BomComponent> frontPage, BomComponent bomComponent){
        Page result;
        try {
            String site = CommonMethods.getSite();
            EntityWrapper<BomComponent> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(bomComponent);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("bomBo", frontPage.getGlobalQuery())
                    .or().like("componentGbo", frontPage.getGlobalQuery())
                    .or().like("enabled", frontPage.getGlobalQuery())
                    .or().like("useItemDefaults", frontPage.getGlobalQuery())
                    .or().like("preAssembled", frontPage.getGlobalQuery())
                    .or().like("assyDataTypeBo", frontPage.getGlobalQuery())
                    .or().like("testPart", frontPage.getGlobalQuery())
                    .or().like("bomTemplate", frontPage.getGlobalQuery())
                    .or().like("disassembleOperationBo", frontPage.getGlobalQuery())
                    .or().like("createTrackableSfc", frontPage.getGlobalQuery())
                    .or().like("assembleAsReq", frontPage.getGlobalQuery())
                    .or().like("autoConsumeKit", frontPage.getGlobalQuery())
                    .or().like("bomComponentType", frontPage.getGlobalQuery())
                    .or().like("orderItemNumber", frontPage.getGlobalQuery())
                    .or().like("erpChangeNumber", frontPage.getGlobalQuery())
        ;
            }
            result = bomComponentService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getBomComponentPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param bomComponent  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult bomComponentSave(@RequestBody BomComponent bomComponent) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = bomComponentService.insertOrUpdate(bomComponent) ? 1 : 0;
        } catch (Exception e) {
            logger.error("bomComponentSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : bomComponent);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult bomComponentDelete(@PathVariable("id") String id){
        int count;
        try {
            count = bomComponentService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("bomComponentDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = bomComponentService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("bomComponentBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}